//
//  SomeTingGet.swift
//  BellManage
//
//  Created by Bestsch on 16/3/30.
//  Copyright © 2016年 wsl. All rights reserved.
//

import Foundation

/**
 检查是否为手机号码
 
 - parameter num: 手机号码
 
 - returns: 是否为手机号码
 */
func isPhoneNum(_ num: String) ->Bool{
//    let mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"
//    let  CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
//    let  CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$"
//    let  CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$"
    let CM = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$"
    let CU = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$"
    let CT = "^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$"
//    let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
    let regextestcm = NSPredicate(format: "SELF MATCHES %@",CM )
    let regextestcu = NSPredicate(format: "SELF MATCHES %@" ,CU)
    let regextestct = NSPredicate(format: "SELF MATCHES %@" ,CT)
    if ( (regextestcm.evaluate(with: num)  == true)
        || (regextestct.evaluate(with: num) == true)
        || (regextestcu.evaluate(with: num) == true))
    {
        return true
    }
    else
    {
        return false
    }
}

/**
 获取手机的具体型号
 
 - returns: 手机型号
 */
func getPhoneMode() -> String? {
    let name = UnsafeMutablePointer<utsname>.allocate(capacity: 1)
    uname(name)
    let machine = withUnsafePointer(to: &name.pointee.machine, { (ptr) -> String? in
        
        let int8Ptr = unsafeBitCast(ptr, to: UnsafePointer<CChar>.self)
        return String(cString: int8Ptr)
    })
    name.deallocate(capacity: 1)
    if let deviceString = machine {
        switch deviceString {
        //iPhone
        case "iPhone1,1":
            return "iPhone 1G"
        case "iPhone1,2":
            return "iPhone 3G"
        case "iPhone2,1":
            return "iPhone 3GS"
        case "iPhone3,1", "iPhone3,2":
            return "iPhone 4"
        case "iPhone4,1":
            return "iPhone 4S"
        case "iPhone5,1", "iPhone5,2":
            return "iPhone 5"
        case "iPhone5,3", "iPhone5,4":
            return "iPhone 5C"
        case "iPhone6,1", "iPhone6,2":
            return "iPhone 5S"
        case "iPhone7,1":
            return "iPhone 6 Plus"
        case "iPhone7,2":
            return "iPhone 6"
        case "iPhone8,1":
            return "iPhone 6s"
        case "iPhone8,2":
            return "iPhone 6s Plus"
        default:
            return deviceString
        }
    } else {
        return nil
    }
}


